package cn.jobstart.task;

import cn.jobstart.common.Const;
import cn.jobstart.common.NumberUtil;
import cn.jobstart.common.StringUtil;
import cn.jobstart.common.redis.RedisUtil;
import cn.jobstart.enums.ResumeBasicEducationFlagEnum;
import cn.jobstart.model.ResumeBasic;
import cn.jobstart.modelExt.ResumeBasicExt;
import cn.jobstart.pubInter.AbstractTask;
import cn.jobstart.pubInter.BaseSearch;
import cn.jobstart.pubInter.exception.SkException;
import cn.jobstart.searchExt.ResumeBasicSearchExt;
import cn.jobstart.service.IResumeBasicService;
import cn.jobstart.vo.KeyValueVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author sunke
 * @version 1.0
 * @Description 年龄分布
 * @date 2023-09-28 14:53
 */

@EnableScheduling
@Slf4j
@Component
public class AgeTask extends AbstractTask {

    @Resource
    private IResumeBasicService resumeBasicService;


    @Resource
    private RedisUtil redisUtil;


    @Scheduled(cron = "0 0 04 * * ?")
   // @Scheduled(cron = "0 0/1 * * * ?")
    @Override
    public void run() {

        try {
            ResumeBasicSearchExt search = ResumeBasicSearchExt.getInstance();

            search.setGroupBy(ResumeBasic.BIRTHDAY);
            search.setPageSize(-1);
            Set<String> fieldSet = new HashSet<>();
            fieldSet.add("count(*) as rb_sort_num"); //临时存放
            fieldSet.add("TIMESTAMPDIFF(YEAR,rb_birthday,now()) div 5  as rb_age ");
            search.setCustomSql("rb_birthday is not null  and TIMESTAMPDIFF(YEAR,rb_birthday,now()) div 5 >0 " +
                    "and TIMESTAMPDIFF(YEAR,rb_birthday,now()) div 5  <20 ");
            search.setGroupBy("TIMESTAMPDIFF(YEAR,rb_birthday,now()) div 5");
            search.setOrderBy("TIMESTAMPDIFF(YEAR,rb_birthday,now()) div 5");
            search.setOrderDesc(BaseSearch.ASC_ORDER);



            List<ResumeBasicExt> list = resumeBasicService.getShowFieldList(fieldSet, search);
            if (StringUtil.isNullOrEmpty(list)) {
                return;
            }

            LinkedHashMap<String, Integer> dataMap = new LinkedHashMap<>();

            list.stream().forEach(item -> {
                int age=item.getAge();
                int count = NumberUtil.parseInt(item.getSortNum());
                String key="";
                if(age<4){
                    key="20以下";
                }else if(age<5){
                    key="20-25";
                }else if(age<6){
                    key="25-30";
                }else if(age<7){
                    key="30-35";
                }else if(age<8){
                    key="35-40";
                }else {
                    key="40以上";
                }

                if (dataMap.containsKey(key)) {
                    count = count + NumberUtil.parseInt(dataMap.get(key));
                }
                dataMap.put(key, count);
            });

            final Map<String, Integer> sortedByEdu = dataMap.entrySet()
                    .stream()
                    .sorted(Comparator.comparing((Map.Entry<String, Integer> item) -> ResumeBasicEducationFlagEnum.getDescByType(item.getKey())).reversed())
                    .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));


            redisUtil.lRemoveAll(Const.REDIDS_KEY_AGE);
            sortedByEdu.forEach((k,v)->{

                redisUtil.lSet(Const.REDIDS_KEY_AGE, new KeyValueVO(k,v));
               // keyValueList.add(new KeyValueVO(k,v));
            });






         //   redisUtil.lSetAll(Const.REDIDS_KEY_EDUCATION, keyValueList);


            //     System.out.println(redisUtil.hmget(Const.REDIDS_KEY_EDUCATION));


        } catch (SkException e) {

        }


    }
}
